package com.xmtlxx.web.controller.wms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.exception.ServiceException;
import com.xmtlxx.web.controller.wms.service.SmartCabinetService;
import com.xmtlxx.wms.domain.SmartCabinetCode;
import com.xmtlxx.wms.domain.bo.VerifyOpenAllCabinetBo;
import com.xmtlxx.wms.domain.bo.VerifyOutProBo;
import com.xmtlxx.wms.mapper.SmartCabinetCodeMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 智能柜实现
 *
 * @author hergreen
 * @date 2024/10/10 16:01
 **/
@Service
@RequiredArgsConstructor
public class SmartCabinetServiceImpl implements SmartCabinetService {

    private final SmartCabinetCodeMapper smartCabinetCodeMapper;

    @Override
    public boolean generateSmartCabinetCode(Integer type,String id) {

        return smartCabinetCodeMapper.generateCode(type,id);
    }

    @Override
    public boolean batchGenerateSmartCabinetCode(Integer type, List<String> ids) {

        return smartCabinetCodeMapper.batchGenerateCode(type,ids);
    }

    @Override
    public TableDataInfo<VerifyOutProBo> getVerifyOutPro(PageQuery pageQuery, VerifyOutProBo bo) {

        QueryWrapper<Object> queryWrapper = new QueryWrapper<>()
                .eq(StrUtil.isNotBlank(bo.getPno()), "I.PNO", bo.getPno());
        Page<VerifyOutProBo> verifyOutPro = smartCabinetCodeMapper.getVerifyOutPro(pageQuery.build(), queryWrapper);
        return TableDataInfo.build(verifyOutPro);
    }

    @Override
    public TableDataInfo<VerifyOpenAllCabinetBo> getVerifyOpenAllCabinet(PageQuery pageQuery) {

        Page<VerifyOpenAllCabinetBo> verifyOpenAllCabinet = smartCabinetCodeMapper.getVerifyOpenAllCabinet(pageQuery.build());
        return TableDataInfo.build(verifyOpenAllCabinet);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Void> verifySmartCabinetCode(String objId, String code, Integer type) {

        //get status is 0 of code and expiration time lt now
        SmartCabinetCode newestCode = smartCabinetCodeMapper.getNewestCode(objId, type);

        if (newestCode == null) {
            return R.fail("验证码过期或不存在");
        }

        //verify count  + 1
        newestCode.setCount(newestCode.getCount() + 1);

        if (newestCode.getCode().equals(code)) {
            //verify success status set 1
            newestCode.setStatus("1");
            if (smartCabinetCodeMapper.updateById(newestCode) == 0) {
                throw new ServiceException("系统错误请重新验证");
            }
            return R.ok("验证成功");
        } else {

            //verify count > 3 then status = 2
            if (newestCode.getCount() > 2) {
                //verify fail status set 2
                newestCode.setStatus("2");
            }

            if (smartCabinetCodeMapper.updateById(newestCode) == 0) {
                throw new ServiceException("系统错误请重新验证");
            }

            String msg = newestCode.getCount() > 2 ? "验证码输入错误3次,请联系相关人员重新生成" :
                    "第" + newestCode.getCount() + "次验证失败,请输入正确的验证码";

            return R.fail(msg);
        }
    }

}
